package com.saile.service;

import com.saile.dao.MessageBoardMapper;
import com.saile.domain.MessageBoard;
import com.saile.domain.User;
import com.saile.myRunTime.MyRunTimeExcption;
import com.saile.tool.HttpClientHelper;
import com.saile.tool.json.JsonHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-4-24.
 */
@Service
public class MessageBoardServiceImpl extends BaseService implements MessageBoardService {

    @Autowired
    MessageBoardMapper messageBoardMapper;

    private void   insertMessageBoard(MessageBoard messageBoard){
        messageBoardMapper.insertMessageBoard(messageBoard);
    }private void   updateMessageBoard(MessageBoard messageBoard){
        messageBoardMapper.updateMessageBoardByPrimaryKeySelective(messageBoard);
    }
    @Override
    public void modifyMessageBoard(MessageBoard messageBoard) throws MyRunTimeExcption {
if(messageBoard.getTid()==null){
    insertMessageBoard(messageBoard);
    return;
}
updateMessageBoard(messageBoard);
    }

    @Override
    public List<MessageBoard> getMessageBoardList(Map<String, String> map) throws MyRunTimeExcption {

        List<MessageBoard> messageBoardList = messageBoardMapper.getMessageBoardList(map);

        for (MessageBoard messageBoard : messageBoardList
                ) {
            User user = getUser(messageBoard.getUserId());
            messageBoard.setUserName(user.getNickName());
            String headPic = getHeadPic(messageBoard.getUserId());
            messageBoard.setHeadPic(headPic);
        }
        return messageBoardList;
    }

    private String getHeadPic(Long userId) {
        String url = "http://api.saile.cc/public/getavatarbyuid?uid=" + userId;
        String headPic = "";
        try {
            String resObj = HttpClientHelper.doGet(url);
            logger.info("resobj=getHeadPic=>"+resObj);
            Map<String, Object> m = JsonHandler.json2Map(resObj);
            String data = m.get("data").toString();
            Map<String, Object> m2 = JsonHandler.json2Map(data);
            headPic = (String) m2.get("avatar");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return headPic;
    }

    ;

    private User getUser(Long userId) {
        User user = new User();
        String url = "http://api.saile.cc/public/getUserById?uid=" + userId;
        try {
            String resObj = HttpClientHelper.doGet(url);
            logger.info("resobj=getUser=>"+resObj);
            Map<String, Object> m = JsonHandler.json2Map(resObj);
            String data = m.get("data").toString();
            Map<String, Object> m2 = JsonHandler.json2Map(data);
            user.setNickName(String.valueOf(m2.get("nickname")));
            user.setTid(Long.parseLong(String.valueOf((int) m2.get("user_id"))));
            user.setUserName(String.valueOf(m2.get("username")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public MessageBoard getMessageBoard(Map<String, String> map) throws MyRunTimeExcption {
        return messageBoardMapper.getMessageBoard(map);
    }
}
